/**
 * 
 */
package openbang.chatroom.web.widget;

import java.util.List;

import openbang.chatroom.service.intf.ChatService;
import openbang.chatroom.service.vo.ChatGroup;
import openbang.chatroom.service.vo.ChatServiceEnum;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * 显示聊天室的列表
 * 
 * 可以加入
 * 可以新建
 * 
 * DateTimeFormat.getFormat("yyyy-MM-dd HH:mm").format
 * @author aibo zeng
 *
 */
public class ViewChatRoomList extends Composite {	
	
	//根
	private VerticalPanel vPanelRoot = new VerticalPanel();
	
	Grid userGrid = new Grid();
	
	Button newBtn = new Button("创建聊天室");
	
	ChatRoomInfoDialog chatRoomInfoDialog = null;
	
	private String defaultNiceName = "";
	private String defaultGroupId = ""; //当前操作的组
	
	public ViewChatRoomList() {
		initWidget(vPanelRoot);
		vPanelRoot.add(userGrid);
		vPanelRoot.add(newBtn);
		newBtn.setEnabled(false);
		newBtn.setStyleName("btnTxt");
	}

	public void initData(List<ChatGroup> groups) {
		userGrid.setStyleName("chatRoomListTable");
		userGrid.resize(groups.size()+1,5);
		userGrid.setHTML(0,0,"<center><b>聊天室主题</b></center>");
		userGrid.setHTML(0,1,"<center><b>创建者</b></center>");
		userGrid.setHTML(0,2,"<center><b>创建时间</b></center>");
		userGrid.setHTML(0,3,"<center><b>有无密码</b></center>");
		userGrid.setHTML(0,4,"<center><b></b></center>");
		int row = 1; //从起始位1开始
		for(ChatGroup cg : groups){
			HTML subject = new HTML(cg.getSubject());
			subject.setTitle(cg.getNiceName()+ ","+DateGwtHelper.format(cg.getCreateDate()));
			userGrid.setWidget(row, 0,subject );
			userGrid.setText(row, 1,cg.getNiceName() );
			userGrid.setText(row, 2,DateGwtHelper.format(cg.getCreateDate()) );
			userGrid.setText(row, 3,cg.getPassword().length()<=0?"无密码":"有密码");
			HTML join = new HTML("<a href=\"#\">加入</a>");
			join.addClickHandler(new JoinClickHandle(cg));
			userGrid.setWidget(row, 4,join);			
			row++;
		}
		newBtn.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				initDialog();
				chatRoomInfoDialog.initData("创建聊天室", new ChatGroup(), "create",defaultNiceName);
				chatRoomInfoDialog.show();
				chatRoomInfoDialog.center();				
			}
		});
		newBtn.setEnabled(true);
	}

	private class JoinClickHandle implements ClickHandler{
		ChatGroup group = null;
		public JoinClickHandle(ChatGroup group){
			this.group = group;
		}
		public void onClick(ClickEvent event) {
			initDialog();
			chatRoomInfoDialog.initData("加入聊天室", group, "join",defaultNiceName);
			chatRoomInfoDialog.show();
			chatRoomInfoDialog.center();
		}
	}
	private MyChatHandler myChatHandler = new MyChatHandler();
	public void initDialog(){
		if(chatRoomInfoDialog==null){
			chatRoomInfoDialog = new ChatRoomInfoDialog();
		}
		if(myChatHandler==null){
			myChatHandler = new MyChatHandler();
		}
		chatRoomInfoDialog.setChatHandler(myChatHandler);
	}
	
	//获取 在 JSP的 javascript中定义的 跳转页面
	public static native void openWinToChatInHtml(String groupId , String niceName) /*-{  
         return parent.openWinToChat(groupId,niceName);
    }-*/;	    

	private class MyChatHandler implements OpenBangChatHandler{
		@Override
		public void onCreate(ChatGroup chatGroup) {
			defaultNiceName = chatGroup.getNiceName();
			ChatService.Util.getInstance().createGroup(chatGroup, new AsyncCallback<String>() {
				@Override
				public void onSuccess(String result) {
					if(result.indexOf("OK")==0){
					  //Window.alert("创建成功,单击<加入>进入您的聊天室");
					  //跳到聊天页面
					  defaultGroupId = result.substring(2);
					  openWinToChatInHtml(defaultGroupId,defaultNiceName);
					  //刷新组列表
					  Window.Location.reload();
					}
					else{
						int code = Integer.parseInt(result.substring(2));
						Window.alert("创建失败:"+ChatServiceEnum.getDesc(code));
					}
				}
				
				@Override
				public void onFailure(Throwable caught) {
					Window.alert(caught.getMessage());
				}
			});
		}

		@Override
		public void onJoin(ChatGroup chatGroup,String niceName) {
			defaultGroupId = chatGroup.getGroupId();
			defaultNiceName = niceName;
			ChatService.Util.getInstance().joinGroup(chatGroup.getGroupId(), niceName, chatGroup.getPassword(), new AsyncCallback<Integer>() {
				@Override
				public void onSuccess(Integer result) {
					if(ChatServiceEnum.OK.getCode()==result){				
						//跳到聊天页面
						openWinToChatInHtml(defaultGroupId,defaultNiceName);
					}
					else{
						Window.alert("加入成功");
					}
				}
				
				@Override
				public void onFailure(Throwable caught) {
					Window.alert(caught.getMessage());
				}
			});
		}
	}
}
